package com.prs.crm.action.crm.report;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.jfree.data.general.AbstractDataset;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.crm.CustomerService;

public class CustomerAddReportAction extends ReportAction {

	private static final long serialVersionUID = 341510994480531151L;
	private CustomerService customerService;
	private ChartService chartService;
	private ChartResult chartResult;
	private PaginationSupport pagination;
	private int exportType;
	private int state;
	private String year;
	private String month;
	private String category;
	
	private Date beginTime;
	private Date endTime;
	

	@Action("editCustomerAdd")
	public String editCustomerAdd() {
		return SUCCESS;
	}

	@Action("listCustomerAdd")
	public String listCustomerAdd() {
		category = ReportUtils.URLDecode(this.getCategory());
		Date dates = null;
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Customer.class);
		if(category != null && category.indexOf("周") != -1 && this.getState() == 2){
			String week = category.substring(0, category.indexOf("周"));
			int day = Integer.parseInt(week) * 7;
			int monthDay = ReportUtils.getDaysByMonth(Integer.parseInt(this.getYear()), Integer.parseInt(this.getMonth()));
			if(day > monthDay){
				int temp = monthDay - (7 - (day - monthDay));
				dates = ReportUtils.convertToDate(this.getYear(), this.getMonth(), temp+"");
				if(dates != null){
					criteria.add(Restrictions.ge("recordTime", dates));
					beginTime = dates;
					endTime = ReportUtils.convertToDate(this.getYear(), ""+(Integer.parseInt(this.getMonth())+1));
					criteria.add(Restrictions.lt("recordTime", endTime));
				}
			}else{
				endTime = ReportUtils.convertToDate(Integer.parseInt(year), Integer.parseInt(month), day,23,59,59);
				int bengin = day - 7 == 0 ? 1 : day - 7;
				beginTime = ReportUtils.convertToDate(this.getYear(), this.getMonth(), bengin+"");
				if(beginTime != null)
					criteria.add(Restrictions.ge("recordTime", beginTime));
				if(endTime != null)
					criteria.add(Restrictions.le("recordTime", endTime));
			}
		}else if(category != null && category.indexOf("月") != -1 && this.getState() == 1){
			String mon = category.substring(0, category.indexOf("月"));
			dates = ReportUtils.convertToDate(this.getYear(), mon);
			if (dates != null) {
				criteria.add(Restrictions.ge("recordTime", dates));
				beginTime = dates;
				Calendar c = Calendar.getInstance();
				c.setTime(dates);
				c.add(Calendar.MONTH, 1);
				endTime = c.getTime();
				criteria.add(Restrictions.lt("recordTime", endTime));
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "viewCustomerAddPie", results = { @Result(name = "success", type = "chain", location = "editCustomerAdd") })
	public String viewCustomerAddPie() {
		Date dates = null;
		if (this.getState() == 1) {
			dates = ReportUtils.convertToDate(this.getYear());
		} else {
			dates = ReportUtils.convertToDate(this.getYear(), this.getMonth());
		}
		Collection<Customer> dataList = this.getCustomerService()
				.getCustomerAddReportSouce(dates, state);
		AbstractDataset data = chartService.fillCustomerAddLine(dataList,
				state, year, month, Constrains.REPORT_PIECHART, null);
		chartResult = chartService.export(data, "客户新增走势图", "listCustomerAdd");
		return SUCCESS;
	}

	@Action(value = "viewCustomerAddBar", results = { @Result(name = "success", type = "chain", location = "editCustomerAdd") })
	public String viewCustomerAddBar() {
		Date dates = null;
		if (this.getState() == 1) {
			dates = ReportUtils.convertToDate(this.getYear());
		} else {
			dates = ReportUtils.convertToDate(this.getYear(), this.getMonth());
		}
		Collection<Customer> dataList = this.getCustomerService()
				.getCustomerAddReportSouce(dates, state);
		AbstractDataset data = chartService.fillCustomerAddLine(dataList,
				state, year, month, Constrains.REPORT_BARCHART, null);
		chartResult = chartService.export(data, "客户新增走势图", "增量", "",
				"listCustomerAdd");
		return SUCCESS;
	}

	@Action(value = "viewCustomerAddLine", results = { @Result(name = "success", type = "chain", location = "editCustomerAdd") })
	public String viewCustomerAddLine() {
		Date dates = null;
		if (this.getState() == 1) {
			dates = ReportUtils.convertToDate(this.getYear());
		} else {
			dates = ReportUtils.convertToDate(this.getYear(), this.getMonth());
		}
		Collection<Customer> dataList = this.getCustomerService()
				.getCustomerAddReportSouce(dates, state);
		AbstractDataset data = chartService.fillCustomerAddLine(dataList,
				state, year, month, Constrains.REPORT_BARCHART, "客户新增走势图");
		chartResult = chartService.exportLineChart(data, "客户新增走势图", "增量", "",
				"listCustomerAdd");
		return SUCCESS;
	}
	
	@Action(value = "exportCustomerAddReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportCustomerAddReport(){
		ReportTable table = new ReportTable("客户新增走势报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("客户名", "name"));
		table.addCell(new ReportCell("客户编号", "code"));
		table.addCell(new ReportCell("客户类型", "type.name"));
		table.addCell(new ReportCell("行业", "trade.name"));
		table.addCell(new ReportCell("客户级别", "level.name"));
		table.addCell(new ReportCell("客户价值", "merit"));
		table.addCell(new ReportCell("联系电话", "officePhone"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Customer.class);
		if(beginTime != null){
			criteria.add(Restrictions.ge("recordTime", beginTime));
		}
		if(endTime != null){
			criteria.add(Restrictions.lt("recordTime", endTime));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}
	

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public int getExportType() {
		return exportType;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

}
